package com.yegrina.member.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yegrina.member.dao.MemberDao;
import com.yegrina.member.domain.Member;
import com.yegrina.member.validator.MemberValidator;

@Controller
public class loginController {
		
	private MemberDao memberDao;
	private String formViewName = "index";
	
	@Autowired
	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
	
	@RequestMapping(value="/membership/login.do", method=RequestMethod.GET)
	public String form(){
		System.out.println("get");
		return formViewName;
		
	}
	
	@ModelAttribute
	public Member formBacking() {
		return new Member();
	}
	
	@RequestMapping(value="/membership/login.do", method=RequestMethod.POST)
	public String submit(Member memberCommand, BindingResult result){
		System.out.print("post");
		new MemberValidator().loginValidate(memberCommand, result);
		
		try{
		if(result.hasErrors()){
			System.out.print("validate");
			return formViewName;
		}

		if(memberCommand.getUserpasswd().equalsIgnoreCase(memberDao.getPasswd(memberCommand.getUserid()))){
			
			return "twtMain";
		}else{
			result.reject("notmatch","notmatchMsg");
			return formViewName;
		}
		
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return formViewName;
		
	}
}